#!/usr/bin/env python

'''Implemention of RFC876 or Daytime Protocol

See http://tools.ietf.org/html/rfc867.'''

import socket
import time
import logging

HOSTNAME='localhost'
TCP_PORT=13
UDP_PORT=13

def tcp_server(hostname=HOSTNAME, port=TCP_PORT):
    logging.info('Starting Daytime TCP server on host %s port %d' % (hostname, port))
    service = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    service.bind((hostname, port))
    service.listen(1)
    logging.info('Accepting connections')

    while True:
        channel, info = service.accept()
        logging.info("Connection from %s" % info[0])
        now = time.asctime()
        channel.send(now)
        logging.info("Daytime sent")
        channel.close()
        logging.info("Connection closed")                                                      
if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    tcp_server(port=1313)
